// 与数据库打交道
const { create, update, del, list, get } = require('../model/sys')
const { Success } = require('../response/code')
class SysService {
  async create(ctx) {
    console.log('service create')
    //
    try {
      const result = await create('测试')
      ctx.body = new Success(result).toString()
    } catch (err) {
      console.log(err)
      // ctx.app.emit('error', { message: err })
    }
  }

  async update(ctx) {
    console.log('service update')
    try {
      const result = await update(1, '测试更新')
      console.log(result, '<<< result')
      ctx.body = result
    } catch (err) {
      console.log(err)
      ctx.app.emit('error', { message: err })
    }
  }

  async get(ctx) {
    console.log('service get')
    try {
      const result = await get(1)
      ctx.body = result
      console.log(result, '<<< result')
    } catch (err) {
      console.log(err)
      ctx.app.emit('error', { message: err })
    }
  }

  async del(ctx) {
    console.log('service del')
    try {
      const result = await del(1)
      console.log(result, '<<< result')
      ctx.body = result
    } catch (err) {
      console.log(err)
      ctx.app.emit('error', { message: err })
    }
  }

  async list(ctx) {
    console.log('service list')
    try {
      const result = await list()
      console.log(result, '<<< result')
      ctx.body = result
    } catch (err) {
      console.log(err)
      ctx.app.emit('error', { message: err })
    }
  }
}
module.exports = new SysService()
